package com.waiterxiaoyy.backandroiddesign.utils.poi;

import com.waiterxiaoyy.backandroiddesign.entity.DaKa;
import com.waiterxiaoyy.backandroiddesign.entity.Member;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.NumberToTextConverter;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.xml.crypto.Data;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author :WaiterXiaoYY
 * @description: TODO
 * @data :2021/1/14 22:46
 */
public class DakaPoi {
    private static String filepath = "C:\\Users\\asus\\Desktop\\打卡数据\\";

    public static List<DaKa> getDaka(String filename) throws IOException {
        InputStream inputStream = new FileInputStream(filepath + filename + ".xlsx");
        XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
        Sheet sheet = workbook.getSheetAt(2);
        int maxRowNum = sheet.getPhysicalNumberOfRows();
        List<DaKa> daKaList = new ArrayList<>();
        Long thistime = Long.parseLong(filename);
        for(int i = 1; i < maxRowNum; i++) {
            DaKa daKa = new DaKa();
            Row row = sheet.getRow(i);
            String grade = getCellStringValue(row.getCell(1));
            String student = getCellStringValue(row.getCell(2));
            String studentid = getCellStringValue(row.getCell(3));
            String phone = getCellStringValue(row.getCell(4));
            String classname = getCellStringValue(row.getCell(6));
            daKa.setGrade(grade);
            daKa.setStudent(student);
            daKa.setStudentid(studentid);
            daKa.setClassname(classname);
            daKa.setPhone(phone);
            daKa.setThistime(thistime);
            daKaList.add(daKa);
        }
        return daKaList;
    }
    public static void main(String[] args) throws IOException {
        InputStream inputStream = new FileInputStream(filepath + "2021011403" + ".xlsx");
        XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
        Sheet sheet = workbook.getSheetAt(2);
        int maxRowNum = sheet.getPhysicalNumberOfRows();
        List<DaKa> daKaList = new ArrayList<>();
        for(int i = 1; i < maxRowNum; i++) {
            DaKa daKa = new DaKa();
            Row row = sheet.getRow(i);
            String grade = getCellStringValue(row.getCell(1));
            String student = getCellStringValue(row.getCell(2));
            String studentid = getCellStringValue(row.getCell(3));
            String phone = getCellStringValue(row.getCell(4));
            String classname = getCellStringValue(row.getCell(6));
            daKa.setGrade(grade);
            daKa.setStudent(student);
            daKa.setStudentid(studentid);
            daKa.setClassname(classname);
            daKa.setPhone(phone);

        }
    }

    public static String getCellStringValue(Cell cell) {
        if (cell == null) {
            return "";
        }
        int type = cell.getCellType();
        String cellValue;
        switch (type) {
            case 3:
                cellValue = "";
                break;
            case 5  :
                cellValue = "";
                break;
            case 4:
                cellValue = String.valueOf(cell.getBooleanCellValue());
                break;
            case 0:
                cellValue = NumberToTextConverter.toText(cell.getNumericCellValue());
                break;
            case 1:
                cellValue = cell.getStringCellValue();
                break;
            case 2:
                cellValue = cell.getCellFormula();
                break;
            default:
                cellValue = "";
                break;
        }
        return cellValue;
    }
}
